﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Cherry.Agile.RpcService
{
    /// <summary>
    /// 扩展读xml节点的方法
    /// create by laiguoxin@szxys.cn
    /// </summary>
    public static class XmlConfigExtend
    {
        public static string Get(this XmlNode node, string attributeName, string defaultValue)
        {
            return node.Attributes[attributeName] == null ? defaultValue : node.Attributes[attributeName].Value;
        }
        public static string Get(this XmlNode node, string attributeName)
        {
            return node.Attributes[attributeName] == null ? string.Empty : node.Attributes[attributeName].Value;
        }
        public static bool GetBool(this XmlNode node, string attributeName)
        {
            return node.Attributes[attributeName] == null ? false : Convert.ToBoolean(node.Attributes[attributeName].Value);
        }
        public static bool GetBool(this XmlNode node, string attributeName, bool defaultValue)
        {
            return node.Attributes[attributeName] == null ? defaultValue : Convert.ToBoolean(node.Attributes[attributeName].Value);
        }

        public static void AddOrSet<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)
        {
            if (dict.ContainsKey(key))
                dict[key] = value;
            else
                dict.Add(key, value);
        }
    }

}
